home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Snippets / Sound / Speech Recognition sample / _source / Language.c next >
Encoding:
C/C++ Source or Header  |  1996-11-15  |  7.0 KB  |  263 lines  |  [TEXT/CWIE]

  1. #ifndef __LANGUAGE__
  2. #include "Language.h"
  3. #endif
  4.  
  5. OSErr        MakeNewLanguage        (SpeechInfoPtr theSpeechInfo)
  6. {
  7.     OSErr        theErr        = noErr;
  8.  
  9.     /* Make language models */
  10.     theErr = ReadInLanguages (theSpeechInfo);
  11.  
  12.     /* Use this LM in recognition */
  13.     if (theErr == noErr) {
  14.         theErr = SRSetLanguageModel (theSpeechInfo->theRecognizer, theSpeechInfo->languages->theLanguage);
  15.     }
  16.  
  17.     return theErr;
  18. }
  19.  
  20. OSErr    ReadInLanguages            (SpeechInfoPtr theSpeechInfo)
  21. {
  22.     LanguageModelListPtr    currentLang            = nil;
  23.     TREEResourcePtr            TREEResPtr            = nil;
  24.     short                    numLanguages        = 0,
  25.                             onlySubPath            = false,
  26.                             i;
  27.     OSErr                    theErr                = noErr;
  28.  
  29.     numLanguages = Count1Resources (kTREEType);
  30.     theErr = ResError ();
  31.  
  32.     theSpeechInfo->languages = (LanguageModelListPtr)NewPtr (sizeof (LanguageModelList));
  33.     theSpeechInfo->languages->nextLanguage = nil;
  34.     currentLang = theSpeechInfo->languages;
  35.  
  36.     for (i = 0; i < numLanguages; i++) {
  37.         theErr = GetTREERes (&TREEResPtr, kBaseResID + i, 1, &onlySubPath);
  38.         DisposePtr ((Ptr)TREEResPtr);
  39.         if (onlySubPath == false) {
  40.             theErr = AddStringsToLanguage (theSpeechInfo, currentLang, kBaseResID + i);
  41.             if (i+1 < numLanguages) {
  42.                 currentLang->nextLanguage = (LanguageModelListPtr)NewPtr (sizeof (LanguageModelList));
  43.                 currentLang = currentLang->nextLanguage;
  44.             }
  45.             else {
  46.                 currentLang->nextLanguage = nil;
  47.             }
  48.             currentLang->resID = kBaseResID + i;
  49.         }
  50.     }
  51.  
  52.     return theErr;
  53. }
  54.  
  55. OSErr    AddStringsToLanguage        (SpeechInfoPtr theSpeechInfo, LanguageModelListPtr currentLang, short resID)
  56. {
  57.     SRLanguageModel            newModel;
  58.     SRPath                    beginningOfPath,
  59.                             restOfPath;
  60.     Str63                    phraseString;
  61.     OSType                    type                = 0;
  62.     TREEResourcePtr            TREEResPtr            = nil;
  63.     CommandPtr                theCommand            = nil;
  64.     long                    refCon                = 0,
  65.                             flags                = 0,
  66.                             ID                    = 0;
  67.     short                    j                    = 1,
  68.                             onlySubPath            = false;
  69.     OSErr                    theErr                = noErr;
  70.     Boolean                    done                = false;
  71.  
  72.     theErr = SRNewLanguageModel (theSpeechInfo->recogSystem, &newModel, nil, 0);
  73.     refCon = resID;
  74.     theErr = SRSetProperty (newModel, kSRRefCon, &refCon, sizeof (refCon));
  75.     /* add each phrase to LM */
  76.     refCon = refCon << 16;
  77.     do {
  78.         theErr = GetTREERes (&TREEResPtr, resID, j, &onlySubPath);
  79.         if (theErr == noErr) {
  80.             theErr = GetFlagsFromTREE (&flags, TREEResPtr);
  81.         }
  82.         if (theErr == noErr && flags == 0) {
  83.             /* Read resource to get top level names to be recognized */
  84.             theErr = GetStringFromTREE (phraseString, TREEResPtr);
  85.             if (phraseString[0] == 0) {
  86.                 theErr = noErr;
  87.                 done = true;    /* Found the last name */
  88.             }
  89.             if (done == false) {
  90.                 theErr = RegisterCommand (TREEResPtr, &theCommand);
  91.                 theErr = SRAddText (newModel, phraseString+1, phraseString[0], (long)theCommand);
  92.             }
  93.         }
  94.         else if (theErr == noErr && flags == 1) {
  95.         /*    This word is the beginning of a path, i.e. "lights X" where X can
  96.             be on, off, dim, etc. */
  97.             theErr = GetTypeFromTREE (&type, TREEResPtr);
  98.             if (theErr == noErr && type == kTREEType) {
  99.                 theErr = GetIDFromTREE (&ID, TREEResPtr);
  100.             }
  101.             if (theErr == noErr) {
  102.                 theErr = SRNewPath (theSpeechInfo->recogSystem, &beginningOfPath);
  103.                 if (theErr == noErr) {
  104.                     theErr = GetStringFromTREE (phraseString, TREEResPtr);
  105.                     if (phraseString[0] == 0) {
  106.                         theErr = noErr;
  107.                         done = true;    /* Found the last name */
  108.                     }
  109.                     if (done == false) {
  110.                         theErr = SRAddText (beginningOfPath, phraseString+1, phraseString[0], 0);
  111.                         if (theErr == noErr) {
  112.                             theErr = GetTREERes (&TREEResPtr, ID, 1, &onlySubPath);
  113.                         }
  114.                         if (theErr == noErr) {
  115.                             theErr = AddStringsToLanguage (theSpeechInfo, currentLang, ID);
  116.                         }
  117.                         if (theErr == noErr) {
  118.                             theErr = SRNewPath (theSpeechInfo->recogSystem, &restOfPath);
  119.                             if (theErr == noErr) {
  120.                                 theErr = SRAddLanguageObject (restOfPath, currentLang->theLanguage);
  121.                             }
  122.                             if (theErr == noErr) {
  123.                                 theErr = SRAddLanguageObject (beginningOfPath, restOfPath);
  124.                             }
  125.                             if (theErr == noErr) {
  126.                                 theErr = SRAddLanguageObject (newModel, beginningOfPath);
  127.                             }
  128.                         }
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.         j++;
  134.     } while (theErr == noErr && done == false);
  135.     if (theErr == resNotFound) {
  136.         theErr = noErr;
  137.     }
  138.     if (theErr != noErr) {    /*    release newly created LM if an error occured while adding phrases */
  139.         SRReleaseObject (newModel);
  140.     }
  141.     else {                    /*    return new LM */
  142.     //We don't release the language models because we will be switching between them later
  143.         currentLang->theLanguage = newModel;
  144.     }
  145.  
  146.     return theErr;
  147. }
  148.  
  149. OSErr    GetTREERes                (TREEResourcePtr *theTREEResPtr, short resID, short index, short *onlySubPath)
  150. {
  151.     TREEResourcePtr        TREEResPtr        = nil;
  152.     Handle                TREEHandle        = nil;
  153.     long                offset            = 0;
  154.     OSErr                theErr            = noErr;
  155.     short                itemCount        = 0,
  156.                         i;
  157.  
  158.     TREEHandle = Get1Resource (kTREEType, resID);
  159.     theErr = ResError ();
  160.     if (TREEHandle != nil && theErr == noErr) {
  161.         *onlySubPath = ((short *)*TREEHandle)[0];
  162.         itemCount = ((short *)*TREEHandle)[1];
  163.         if (index > itemCount || index < 1) {
  164.             theErr = resNotFound;
  165.         }
  166.         else {
  167.             offset = 4;        /* account for one time bytes at start of resource */
  168.             for (i = 0; i < index; i++) {
  169.                 TREEResPtr = (TREEResourcePtr)&(*TREEHandle)[offset];
  170.                 offset += 12;    /* account for flags, type, and ID */
  171.                 /* length of string (plus size byte), rounded to a quad byte length */
  172.                 offset += (((TREEResPtr->name[0] + 1) + 3) & ~3);
  173.             }
  174.             *theTREEResPtr = (TREEResourcePtr)NewPtr (sizeof (TREEResource) + TREEResPtr->name[0]);
  175.             if (*theTREEResPtr != nil && MemError () == noErr) {
  176.                 (*theTREEResPtr)->flags    = TREEResPtr->flags;
  177.                 (*theTREEResPtr)->type    = TREEResPtr->type;
  178.                 (*theTREEResPtr)->ID    = TREEResPtr->ID;
  179.                 for (i = 0; i < TREEResPtr->name[0] + 1; i++) {
  180.                     (*theTREEResPtr)->name[i] = TREEResPtr->name[i];
  181.                 }
  182.             }
  183.             ReleaseResource (TREEHandle);
  184.             theErr = ResError ();
  185.         }
  186.     }
  187.  
  188.     return theErr;
  189. }
  190.  
  191. OSErr    RegisterCommand            (TREEResourcePtr theTREEResPtr, CommandPtr *theCommand)
  192. {
  193.     OSErr                theErr            = noErr;
  194.  
  195.     *theCommand = (CommandPtr)NewPtr (sizeof(Command));
  196.  
  197.     theErr = GetFlagsFromTREE (&(*theCommand)->flags, theTREEResPtr);
  198.     theErr = GetTypeFromTREE (&(*theCommand)->type, theTREEResPtr);
  199.     theErr = GetIDFromTREE (&(*theCommand)->ID, theTREEResPtr);
  200.  
  201.     return theErr;
  202. }
  203.  
  204. OSErr    GetFlagsFromTREE        (long *theFlags, TREEResourcePtr theTREEResPtr)
  205. {
  206.     OSErr                theErr            = noErr;
  207.  
  208.     if (theTREEResPtr != nil) {
  209.         *theFlags = theTREEResPtr->flags;
  210.     }
  211.     else {
  212.         theErr = kNilPtrErr;
  213.     }
  214.  
  215.     return theErr;
  216. }
  217.  
  218. OSErr    GetTypeFromTREE            (OSType *theType, TREEResourcePtr theTREEResPtr)
  219. {
  220.     OSErr                theErr            = noErr;
  221.  
  222.     if (theTREEResPtr != nil) {
  223.         *theType = theTREEResPtr->type;
  224.     }
  225.     else {
  226.         theErr = kNilPtrErr;
  227.     }
  228.  
  229.     return theErr;
  230. }
  231.  
  232. OSErr    GetIDFromTREE            (long *theID, TREEResourcePtr theTREEResPtr)
  233. {
  234.     OSErr                theErr            = noErr;
  235.  
  236.     if (theTREEResPtr != nil) {
  237.         *theID = theTREEResPtr->ID;
  238.     }
  239.     else {
  240.         theErr = kNilPtrErr;
  241.     }
  242.  
  243.     return theErr;
  244. }
  245.  
  246. OSErr    GetStringFromTREE        (Str255 theString, TREEResourcePtr theTREEResPtr)
  247. {
  248.     OSErr                theErr            = noErr;
  249.     short                i;
  250.  
  251.     if (theTREEResPtr != nil) {
  252.         for (i = 0; i < theTREEResPtr->name[0] + 1; i++) {
  253.             theString[i] = theTREEResPtr->name[i];
  254.         }
  255.     }
  256.     else {
  257.         theString[0] = 0;
  258.         theErr = kNilPtrErr;
  259.     }
  260.  
  261.     return theErr;
  262. }
  263.